package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import pojo.Merchant;
import util.DBUtils;
import dao.MerchantDao;

public class MerchantDaoImpl extends BaseDaoImpl<Merchant> implements MerchantDao {

	@Override
	public int add(Merchant merchant) {
		String sql = "insert into merchants values(?,?,?,?,?)";
		PreparedStatement pst = null;
		int m = 0;
		Connection conn = DBUtils.createConnection();
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, UUID.randomUUID().toString());
			pst.setString(2, merchant.getName());
			pst.setString(3, merchant.getIdCard());
			pst.setString(4, merchant.getAvatar());
			pst.setString(5, merchant.getStatus());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			logger.error(e);
		}finally{
			DBUtils.close(conn, pst, null);
		}
		return m;
	}

	@Override
	public int delete(String id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int update(Merchant merchant) {
		String sql = "update merchants set id_card=?,avatar=?,status=?, where name=?";
		PreparedStatement pst = null;
		int m = 0;
		Connection conn = DBUtils.createConnection();
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, merchant.getIdCard());
			pst.setString(2, merchant.getAvatar());
			pst.setString(3, merchant.getStatus());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			logger.error(e);
		}finally{
			DBUtils.close(conn, pst, null);
		}
		return m;
	}

	@Override
	public Merchant load(String id) {
		String sql = "select * from merchants where id=?";
		Connection conn = DBUtils.createConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		Merchant merchant = null;
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, id);
			rs = pst.executeQuery();
			if(rs.next()) {
				merchant = new Merchant();
				merchant.setId(rs.getString("id"));
				merchant.setName(rs.getString("name"));
				merchant.setIdCard(rs.getString("id_card"));
				merchant.setAvatar(rs.getString("avatar"));
				merchant.setStatus(rs.getString("status"));
			}
		} catch (Exception e) {
			logger.error(e);
		}finally{
			DBUtils.close(conn, pst, rs);
		}
		return merchant;
	}
	
	public Merchant loadByName(String name) {
		String sql = "select * from merchants where name=?";
		Connection conn = DBUtils.createConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		Merchant merchant = null;
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, name);
			rs = pst.executeQuery();
			if(rs.next()) {
				merchant = new Merchant();
				merchant.setId(rs.getString("id"));
				merchant.setName(rs.getString("name"));
				merchant.setIdCard(rs.getString("id_card"));
				merchant.setAvatar(rs.getString("avatar"));
				merchant.setStatus(rs.getString("status"));
			}
		} catch (Exception e) {
			logger.error(e);
		}finally{
			DBUtils.close(conn, pst, rs);
		}
		return merchant;
	}

	@Override
	public List<Merchant> findAll() {
		String sql = "select * from merchants";
		Connection conn = DBUtils.createConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<Merchant> merchants = new ArrayList<>(); 
		try {
			pst = conn.prepareStatement(sql);
			rs = pst.executeQuery();
			while(rs.next()) {
				Merchant merchant = new Merchant();
				merchant.setId(rs.getString("id"));
				merchant.setName(rs.getString("name"));
				merchant.setIdCard(rs.getString("id_card"));
				merchant.setAvatar(rs.getString("avatar"));
				merchant.setStatus(rs.getString("status"));
				merchants.add(merchant);
			}
		} catch (Exception e) {
			logger.error(e);
		}finally{
			DBUtils.close(conn, pst, rs);
		}
		return merchants;	
	}

	@Override
	public List<Merchant> findByStatus(String status) {
		String sql = "select * from merchants where status=?";
		Connection conn = DBUtils.createConnection();
		PreparedStatement pst = null;
		ResultSet rs = null;
		List<Merchant> merchants = new ArrayList<>(); 
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, status);
			rs = pst.executeQuery();
			while(rs.next()) {
				Merchant merchant = new Merchant();
				merchant.setId(rs.getString("id"));
				merchant.setName(rs.getString("name"));
				merchant.setIdCard(rs.getString("id_card"));
				merchant.setAvatar(rs.getString("avatar"));
				merchant.setStatus(rs.getString("status"));
				merchants.add(merchant);
			}
		} catch (Exception e) {
			logger.error(e);
		}finally{
			DBUtils.close(conn, pst, rs);
		}
		return merchants;	
	}

//	@Override
//	public int saveOrUpdate(Merchant merchant) {
//		// TODO Auto-generated method stub
//		return 0;
//	}

	

}
